//
//  CustomerNavigationController.m
//  app
//
//  Created by 王宇宙 on 2017/12/26.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "CustomerNavigationController.h"

@interface CustomerNavigationController () <UINavigationControllerDelegate>

@end

@implementation CustomerNavigationController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  
  self.delegate=self;
  self.navigationBar.tintColor = [UIColor darkGrayColor];
      //修复自定义返回后手势失效
      //拿到navigationController原有的pop手势
      UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;
      //将自有手势停用
      gesture.enabled = NO;
      //拿到手势的view
      UIView *gestureView = gesture.view;
  
      //创建一个自己的手势
      UIPanGestureRecognizer *popRecognizer = [[UIPanGestureRecognizer alloc] init];
      popRecognizer.delegate = self;
      popRecognizer.maximumNumberOfTouches = 1;
      //将自己创建的手势放到原有手势的view上
      [gestureView addGestureRecognizer:popRecognizer];
//      _panGesture=popRecognizer;
  
      //获取系统的手势的target数组
      NSMutableArray *_targets = [gesture valueForKey:@"_targets"];
      //获取它的唯一对象 有一个叫UIGestureRecognizerTarget的私有类,有一个属性叫_target
      id gestureRecognizerTarget = [_targets firstObject];
      //获取_target:_UINavigationInteractiveTransition 有一个方法叫handleNavigationTranstion
      id navigationInteractiveTransition = [gestureRecognizerTarget valueForKey:@"_target"];
      //从控制台获取出他的方法签名
      SEL handelTransition = NSSelectorFromString(@"handleNavigationTransition:");
      //创建一个与系统一样的手势 我们只把他的类改为UIPanGestureRecognizer
      [popRecognizer addTarget:navigationInteractiveTransition action:handelTransition];
  
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    //这里有两个条件不允许手势 1 当前控制器为根控制器 2 如果这个push  pop 动画正在执行(私有属性)
    return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue];
}


- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
  if (self.childViewControllers.count>0) {
    UIBarButtonItem* back=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    viewController.navigationItem.leftBarButtonItem=back;
    self.interactivePopGestureRecognizer.delegate = (id)viewController;
  }
  [super pushViewController:viewController animated:animated];
}
-(void)back{
  [self popViewControllerAnimated:YES];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  NSString *str = NSStringFromClass([viewController class]);
  if ([str isEqualToString:@"UIViewController"]) {
    [self setNavigationBarHidden:YES animated:YES];
  }else{
    [self setNavigationBarHidden:NO animated:YES];
//    self.navigationBar.backgroundColor = [UIColor clearColor];
//    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationBar setShadowImage:[UIImage new]];
  }
}

@end
